﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using NLBS.Security;
using NLBS.Entity;
using NLBS.Data.Business;
using NLBS.Framework;
using NLBS.Views;
using NLBS.Text;

namespace NLBS.Web.UI
{
    public class CommentCommand : ICommand, ICommentAction<Comment>
    {
        #region Presenter

        private CommentListPresenter _presenter;

        public CommentListPresenter Presenter
        {
            get
            {
                if (this._presenter == null)
                    _presenter = new CommentListPresenter();
                this._presenter.Action = this;
                return _presenter;
            }
        }

        private void PresenterInit()
        {
            this.Presenter.OnActionLoaded();
        }
        #endregion

       

       private Action act;
       private CallBackBase callback;
       private HttpRequest request;
       public CommentCommand(Action act,CallBackBase callBack)
       {
           this.act=act;
           this.callback = callBack;
       }

       #region ICommand Members
       public void Excute()
        {
            request = callback.Page.Request;
            PresenterInit();
            switch (act)
            {
                case Action.Insert:
                    InsertAction();
                    break;
                case Action.Delete:
                    DeleteAction();
                    break;
                case Action.DeleteMulit:
                    DeleteMulitBook();
                    break;
            }
        }

       private void DeleteMulitBook()
       {
           string[] Ids = request.Form["Ids"].Split(new char[] { ',' });
           foreach (string item in Ids)
           {
               this.keyID = int.Parse(item);
               this.DataDelete(null, null);
           }
       }

        private void DeleteAction()
        {
            if (BlogInfo.Instance().CurrentUser.Group.Power.Delete < 1) throw new Exception("没有权限删除评论");
            this.keyID = Convert.ToInt32(request.Form["id"]);
            this.DataDelete(null, null);
        }

        private void InsertAction()
        {
            StringSecurity.CheckSecurityCode(request.Form["scode"]);
            Comment comment = new Comment();
            comment.Author = request.Form["name"];
            comment.Content = HtmlHelper.HtmlSafe(request.Form["message"]);
            comment.AuthorID = 0;
            comment.LogID = Convert.ToInt32(request.Form["logID"]);
            comment.PostTime = DateTime.Now;
            comment.IP = request.UserHostAddress;
            //comment.EditMark = string.Empty;
            comment.UbbFlags = "111111";
            comment.Hidden = false;
            this.Entity = comment;
            this.DataInsert(null, null);
        }

        #endregion

        #region IAction<Comment> Members
        private Comment _entity;
        public Comment Entity
        {
            get
            {
                return _entity;
            }
            set
            {
                _entity = value;
            }
        }

        private int _keyID;
        public int keyID
        {
            get
            {
                return _keyID;
            }
            set
            {
                _keyID = value;
            }
        }

        public event EventHandler DataInsert;

        public event EventHandler DataUpdate;

        public event EventHandler DataDelete;

        public event EventHandler DataSelectSingle;

        #endregion

        #region ICommentAction Members
        private string _passWord;
        public string password
        {
            get
            {
                return RequestWrapper.GetFormString("password");
            }
            set
            {
                _passWord=value;
            }
        }
        private string _username;
        public string username
        {
            get
            {
                return request.Form["name"];
            }
            set
            {
                _username=value;
            }
        }
        private bool _register;
        public bool register
        {
            get
            {
                return RequestWrapper.GetFormbool("register");
            }
            set
            {
                _register=value;
            }
        }


        public string content
        {
            get
            {
               return request.Form["message"];
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        #endregion
    }
}
